package com.scs.application.modules.sys.controller;
import com.scs.application.core.mvc.controller.FlowController;
import com.scs.application.modules.sys.entity.Label;
import com.scs.application.modules.sys.mapper.LabelMapper;
import com.scs.application.modules.sys.model.RestResult;
import com.scs.application.modules.sys.service.LabelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/sys/label")
public class LabelController extends FlowController<LabelService, Label> {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private LabelMapper labelMapper;

    @PostMapping("/getLabelContent")
    RestResult getLabelContent(@RequestBody Label label) {
        HashMap map = labelMapper.getLabelByCodeAndVersion(label.getCode(), label.getVersion());
        if (map == null) {
            logger.error("找不到对应的标签记录，可能是标签版本发生变化，关闭窗口重新打开。");
            return RestResult.error("找不到对应的标签记录，可能是标签版本发生变化，关闭窗口重新打开。");
        }
        logger.info("返回标签内容");
        return RestResult.ok("", map);
    }
    @PostMapping("/saveLabelContent")
    RestResult saveLabelContent(@RequestBody Label label) {
        String labelCode = label.getCode();
        String labelVersion = label.getVersion();
        String labelContent = label.getContent();
        try {
            if (labelMapper.saveLabelContent(labelCode, labelVersion, labelContent) == 1) {
                logger.info("服务端标签保存成功, code = " + labelCode + ", version = " + labelVersion);
            }
            else {
                logger.info("服务端标签保存失败，请检查。code = " + labelCode + ", version = " + labelVersion);
                return RestResult.error("服务端标签保存失败，请检查。code = " + label.getId() + ", version = " + labelVersion);
            }
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
        return RestResult.ok("服务端标签保存成功, code = " + labelCode + ", version = " + labelVersion);
    }
}
